const lodash = require('lodash');
const service = {

    init(tablename, primaryKey, column, addSQL, editSQL, querySQL, listSQL) {
        let camelCaseTableName = lodash.camelCase(tablename);
        let camelCaseTableNameFirst = lodash.upperFirst(camelCaseTableName);
        let telService =
            `const Service = require('egg').Service;
 const lodash = require('lodash');

class ${camelCaseTableNameFirst}Service extends Service {
    /**
     * 新增
     * @param {提交的数据} data 
     * @return 1，0
     */
    async create(data) {
        const { ctx,app } = this;
            let rs = await ctx.model.${camelCaseTableNameFirst}.create(data);
    }

    /**
     * 编辑
     * @param {提交的数据} data 
     * @return 1，0
     */
    async update(data,${primaryKey.t_column_name}) {
        const { ctx,app } = this;
        let rs = await ctx.model.${camelCaseTableNameFirst}.update(data,{ where: { ${primaryKey.t_column_name} } });
    }

    /**
     * 查询列表
     * @param {提交的数据} data 
     * @return 1，0
     */
    async querylist(queryParams,pageSize,page) {
        const { ctx,app } = this;
        queryParams = queryParams || {};
        //方便更改,使用 sql 查询,以下步骤为生产sql的过程
        ${createSQL(querySQL, tablename)}
        let datalist = await ctx.model.query(dataSql,
        { replacements: dataWhereParams, raw: true, type: app.Sequelize.QueryTypes.SELECT });
        //查询总数
        let count = await ctx.model.query(countSql,
        { replacements: whereParams, raw: true, type: app.Sequelize.QueryTypes.SELECT });
        return {
            datalist,
            totalSize:count[0].total
        }
    }

    /**
     * 删除
     * @param {提交的数据} data 
     * @return 1，0
     */
    async delete(id) {
        const { ctx,app } = this;
        let rs = await ctx.model.${camelCaseTableNameFirst}.destroy({"where":{${primaryKey.column_name}:id}});
    }

    /**
     * 查询详情
     * @param {提交的数据} data 
     * @return 1，0
     */
    async queryById(id) {
        const { ctx,app } = this;
        let rs = await ctx.model.${camelCaseTableNameFirst}.findOne({${primaryKey.column_name}:id});
        return rs;
    }
}

module.exports = ${camelCaseTableNameFirst}Service;`;
        return telService;
    }

}

module.exports = service;

function createSQL(querySQL, tablename) {

    let createStep =
        `let whereSQL = "where";
    let whereParams = {};`
    for (var i = 0; i < querySQL.length; i++) {
        createStep = createStep + `\nif (! await app.utils.validator.isEmpty(queryParams['` + querySQL[i].column_name + `'])) {
            whereParams['`+ querySQL[i].column_name + `'] = queryParams['` + querySQL[i].column_name + `'];
            if (whereSQL == "where") {
                whereSQL = whereSQL + ' t.`+ querySQL[i].column_name + ` ` + querySQL[i].query_type + ` :` + querySQL[i].column_name + `';
            } else {
                whereSQL = whereSQL + ' and t.`+ querySQL[i].column_name + ` ` + querySQL[i].query_type + ` :` + querySQL[i].column_name + `';
            }
        }`
    }
    createStep = createStep + `\nif(whereSQL == "where"){
        whereSQL = ""
    }
    //增加页码
    page = (page || 1) * 1;
    pageSize = (pageSize || 10) * 1;
    let dataWhereParams = { ...whereParams };
    dataWhereParams.offset = (page - 1) * pageSize;
    dataWhereParams.limit = pageSize;
    let dataSql = "SELECT * FROM ` + tablename + ` t " + whereSQL + " LIMIT :offset, :limit";
    let countSql = "SELECT count(*) as total FROM ` + tablename + ` t " + whereSQL;`
    return createStep;
}